import { mapGetters, mapActions } from "vuex";
import { getAllDictList } from "@/api/dict";
import { getMenuBtns, getFullUser } from "@/api/login";
// eslint-disable-next-line no-unused-vars
import _ from "lodash";

export default {
  install(Vue) {
    Vue.mixin({
      computed: {
        ...mapGetters([
          "getUserInfo",
          "getActiveMenuIndex",
          "getOpendsKey",
          "getMenuData",
          "getActiveMenuItem",
          "getTags",
          "getDict",
          "getAuthBtns",
          "getToken",
          "getDict"
        ]),
      },
      methods: {
        ...mapActions([
          "setUserInfo",
          "setActiveMenuIndex",
          "setOpendsKey",
          "spliceOpendsKey",
          "setMenuData",
          "setActiveMenuItem",
          "setTags",
          "removeTags",
          "clearTags",
          "setDict",
          "setAuthBtns",
          "setToken",
        ]),
        /**
         * 获取用户信息
         */
        getFullUser() {
          return new Promise((a, b) => {
            getFullUser()
              .then((response) => {
                this.setUserInfo(response);
                a(response);
              })
              .catch(() => {
                b("");
              });
          });
        },
        /***
         * 获取字典的接口
         */
        getAllDictList() {
          return new Promise((a, b) => {
            getAllDictList()
              .then((res) => {
                this.setDict(res);
                a(res);
              })
              .catch(() => {
                b("");
              });
          });
        },
        /**
         * 获取后台菜单权限
         */
        getMenuBtns() {
          return new Promise((a, b) => {
            getMenuBtns()
              .then((res) => {
                const authBtns = res;
                this.setAuthBtns(authBtns);
                a("authBtns");
              })
              .catch(() => {
                b("");
              });
          });
        },
        /**
         * 格式化时间
         */
        formatDate(date) {
          if (date != null) {
            const dateMat = new Date(date);
            const year = dateMat.getFullYear();
            const month =
              dateMat.getMonth() + 1 < 10
                ? "0" + (dateMat.getMonth() + 1)
                : dateMat.getMonth() + 1;
            const day =
              dateMat.getDate() < 10
                ? "0" + dateMat.getDate()
                : dateMat.getDate();
            const timeFormat = year + "-" + month + "-" + day;
            return timeFormat;
          }
        },
        toDecimal2(x) {
          //价格0.00格式
          var f = parseFloat(x);
          if (isNaN(f)) {
            return "-";
          }
          var f = Math.round(x * 100) / 100;
          var s = f.toString();
          var rs = s.indexOf(".");
          if (rs < 0) {
            rs = s.length;
            s += ".";
          }
          while (s.length <= rs + 2) {
            s += "0";
          }
          return s;
        },
        // 数组并集
        uniqueArr(arr1, arr2) {
          const arr3 = arr1.concat(arr2);
          const arr4 = [];
          const ids = [];
          for (let i = 0, len = arr3.length; i < len; i++) {
            if (ids.indexOf(arr3[i].uuid) === -1) {
              ids.push(arr3[i].uuid);
              arr4.push(arr3[i]);
            }
          }
          return arr4;
        },
        // 数组差集
        differenceArr(arr1, arr2) {
          const diff = [];
          const tmp = arr2;
          const ids = [];
          tmp.forEach((ele) => {
            ids.push(ele.uuid);
          });
          arr1.forEach((ele) => {
            if (ids.indexOf(ele.uuid) < 0) {
              diff.push(ele);
            } else {
              tmp.splice(tmp.indexOf(ele), 1);
            }
          });
          return diff.concat(tmp);
        },
        handleSelectionChange(val) {
          this.multipChooseTb = val;
        },
      },
      filters: {
        // 过滤字段
        handleText(value, num) {
          if (!value) return "";
          if (value.length > num) {
            return value.slice(0, num) + "...";
          }
          return value;
        },
        // 数字转化成金额
        formatMoney(val, places, symbol, thousand, decimal) {
          places = !isNaN((places = Math.abs(places))) ? places : 2;
          symbol = symbol !== undefined ? symbol : "¥";
          thousand = thousand || ",";
          decimal = decimal || ".";
          let number = !isNaN(parseFloat(val)) ? parseFloat(val) : 0;
          let negative = number < 0 ? "-" : "";
          let i =
            parseInt((number = Math.abs(+val || 0).toFixed(places)), 10) + "";
          let j = (j = i.length) > 3 ? j % 3 : 0;
          return (
            symbol +
            negative +
            (j ? i.substr(0, j) + thousand : "") +
            i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) +
            (places
              ? decimal +
                Math.abs(number - i)
                  .toFixed(places)
                  .slice(2)
              : "")
          );
        },
      },
    });
  },
};
